package oleg.rms;

/**
 * Copyright 2008,2009 Oleg Frenkel
 *
 * This file is part of PerfectSearch.
 *
 * PerfectSearch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PerfectSearch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PerfectSearch.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * This class simplifies the use of RMS: The Mobile Information 
 * Device Profile provides a mechanism for MIDlets to persistently 
 * store data and later retrieve it.
 */

import java.io.*;
import javax.microedition.rms.RecordStore;

import oleg.C;
import oleg.MainForm;
import oleg.log.Logger;
import oleg.util.StringUtils;

public class RMSManager
{
  static private RMSManager instance = new RMSManager();
  static private String SEPARATOR = "#~#";
  
  private RecordStore recordStore = null;

  // this class is singleton
  private RMSManager() {
    try {
      recordStore = RecordStore.openRecordStore("PerfectSearch",
                                                true, //createIfNecessary
                                                RecordStore.AUTHMODE_ANY,
                                                true);
      // Ensure that we have necessary number of records
      int numOfRecords = recordStore.getNumRecords();
      for (int i = numOfRecords; i < C.RMS_RECORD_NUMBER; i++) {
        recordStore.addRecord(null, 0, 0);
      }
    } catch (Exception ex) {
      Logger.log("RMSManager: failed to open record store: " + 
                 ex.getMessage());
    }
  }

  static public RMSManager instance() {
    return instance;
  }

  public int getInt(int recordId, int defaultValue) {
    DataInputStream is = getRecord(recordId);
    if (is == null) {
      return defaultValue;
    }

    try {
      return is.readInt();
    } catch (Exception ex) {
      return defaultValue;
    }
  }

  public String getString(int recordId, String defaultValue) {
    DataInputStream is = getRecord(recordId);
    if (is == null) {
      return defaultValue;
    }

    try {
      return is.readUTF();
    } catch (Exception ex) {
      return defaultValue;
    }
  }

  public String[] getStringArray(int recordId, String[] defaultValue) {
    String encodedStr = getString(recordId, null);
    if (encodedStr == null) {
      return defaultValue;
    }

    return StringUtils.split(encodedStr, SEPARATOR);
  }

  public void setStringArray(int recordId, String[] value) {
    StringBuffer encodedStr = new StringBuffer();
    for (int i = 0; i < value.length; i++) {
      if (i > 0) {
        encodedStr.append(SEPARATOR);
      }
      encodedStr.append(value[i]);
    }
    setString(recordId, encodedStr.toString());
  }

  public void setInt(int recordId, int value) {
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      DataOutputStream dos = new DataOutputStream(baos);
      dos.writeInt(value);
      setRecord(recordId, baos.toByteArray());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void setString(int recordId, String value) {
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      DataOutputStream dos = new DataOutputStream(baos);
      dos.writeUTF(value);
      setRecord(recordId, baos.toByteArray());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private DataInputStream getRecord(int recordId) {
    try {
      byte[] byteVal = recordStore.getRecord(recordId);
      if (byteVal == null) {
        return null;
      }

      return new DataInputStream(new ByteArrayInputStream(byteVal));
    } catch (Exception ex) {
      return null;
    }
  }

  private void setRecord(int recordId, byte[] byteVal) {
    try {
      recordStore.setRecord(recordId, byteVal, 0, byteVal.length);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

}